#pragma once
#include<iostream>
#include<stdarg.h>
#include<string>
#include<time.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#define SIZE 1024

#define Info 0
#define Debug 1
#define Warning 2
#define Error 3
#define Fatal 4

#define Screen 1
#define Onefile 2
#define Classfile 3

#define LogFile "log.txt"

class Log
{
public:
    Log()
    {
        printMethod = Screen;   // 默认屏幕打印日志
        path = "./log/";
    }   

    void Enable(int method)
    {
        printMethod = method;
    }

    std::string levelToString(int level)
    {
        switch (level)
        {
            case Info: return "Info";
            case Debug: return "Debug";
            case Warning: return "Warning";
            case Error: return "Error";
            case Fatal: return "Fatal";
            default: return "None";
        }
    }

    // void logmessage(int level, const char* format, ...)
    // {
    //     time_t t = time(nullptr);
    //     struct tm *ctime = localtime(&t);
    //     char leftBuffer[SIZE];
    //     snprintf(leftBuffer, sizeof(leftBuffer), "[%s][%d-%d-%d %d:%d:%d]", levelToString(level).c_str(),
    //                 ctime->tm_year + 1900, ctime->tm_mon + 1, ctime->tm_mday,
    //                 ctime->tm_hour, ctime->tm_min, ctime->tm_sec);

    //     va_list s;
    //     va_start(s, format);    // 初始化s，让其指向可变参数部分
    //     char rightBuffer[SIZE * 2];
    //     vsnprintf(rightBuffer, sizeof(rightBuffer), format, s);

    //     char logtxt[SIZE * 2];
    //     snprintf(logtxt, sizeof(logtxt), "%s %s\n", leftBuffer, rightBuffer);

    //     printLog(level, logtxt);
    // }

    void operator()(int level, const char* format, ...)
    {
        time_t t = time(nullptr);
        struct tm *ctime = localtime(&t);
        char leftBuffer[SIZE];
        snprintf(leftBuffer, sizeof(leftBuffer), "[%s][%d-%d-%d %d:%d:%d]", levelToString(level).c_str(),
                    ctime->tm_year + 1900, ctime->tm_mon + 1, ctime->tm_mday,
                    ctime->tm_hour, ctime->tm_min, ctime->tm_sec);

        va_list s;
        va_start(s, format);    // 初始化s，让其指向可变参数部分
        char rightBuffer[SIZE * 2];
        vsnprintf(rightBuffer, sizeof(rightBuffer), format, s);

        char logtxt[SIZE * 3];
        snprintf(logtxt, sizeof(logtxt), "%s %s", leftBuffer, rightBuffer);

        printLog(level, logtxt);
    }

    void printLog(int level, const std::string& logtxt)
    {
        switch(printMethod)
        {
            case Screen:
                std::cout << logtxt << "\n";
                break;
            case Onefile:
                printOneFile(LogFile, logtxt);
                break;               
            case Classfile:
                printClassFile(level, logtxt);
                break;
        }
    }
    
    void printOneFile(const std::string& logName, const std::string& logtxt)
    {
        std::string file = path + logName;
        int fd = open(file.c_str(), O_CREAT|O_WRONLY|O_APPEND, 0666);
        if(fd < 0) return;
        write(fd, logtxt.c_str(), logtxt.size());
        close(fd);
    }

    void printClassFile(int level, const std::string& logtxt)
    {
        std::string file = LogFile;
        file += '.';
        file += levelToString(level);
        printOneFile(file, logtxt);
    }

    ~Log() {}
private:
    int printMethod;
    std::string path;
};

Log lg;